package com.bishe.demo.modules.account.controller;

import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.account.service.UserService;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * post
     * localhost/api/register
     * {"userName":"admin","password":"admin","userPhone":"13245512311","role":{"roleId":1}}
     */
    @PostMapping(value = "/register", consumes = "application/json")
    ResultEntity<User> register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * post
     * localhost/api/login
     * {"userName":"admin","password":"admin"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    ResultEntity<User> login(@RequestBody User user) {
        return userService.login(user);
    }

    /**
     * post
     * localhost/api/login
     * {"userName":"admin","password":"admin"}
     */
    @GetMapping(value = "/logout")
    void logout() {
        userService.logout();
    }

    /**
     * put
     * localhost/api/user
     * {"userId":1,"password":"123456"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    ResultEntity<User> updateUserPassword(@RequestBody User user) {
        return userService.updateUserPassword(user);
    }

    /**
     * put
     * localhost/api/user/v2
     * {"userId":1,"userPhone":"1111111111"}
     */
    @PutMapping(value = "/user/v2", consumes = "application/json")
    ResultEntity<User> updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    /**
     * delete
     * localhost/api/user/1
     */
    @DeleteMapping("/user/{userId}")
    ResultEntity<User> deleteUserByUserId(@PathVariable int userId) {
        return userService.deleteUserByUserId(userId);
    }

    /**
     * get
     * localhost/api/user/1
     */
    @GetMapping("/user/{userId}")
    User getUserByUserId(@PathVariable int userId) {
        return userService.getUserByUserId(userId);
    }

    /**
     * post
     * localhost/api/users
     * {"keyword":"a"}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }
}
